import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

/**
 * @author zfChen
 * @create 2022/7/6 10:59
 */
@Slf4j
public class NettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1、准备eventLoop对象
        EventLoop eventLoop = new NioEventLoopGroup().next();

        // 2、创建promise
        DefaultPromise<Object> promise = new DefaultPromise<>(eventLoop);

        new Thread(() -> {
            try {
                Thread.sleep(1000);
                promise.setSuccess(80);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        // 3、主线程通过future获取结果
        log.info("等待结果");
        log.info("结果是：{}",promise.getNow());
        log.info("结果是：{}",promise.get());
    }
}
